package com.zhong.gang.app.ui.zhuchang.input;

import android.app.Activity;
import android.text.TextUtils;

import com.luck.picture.lib.tools.ToastUtils;
import com.zhong.gang.app.api.Api;
import com.zhong.gang.app.api.HttpUtils;
import com.zhong.gang.app.databinding.ActivityZhuChangBinding;
import com.zhong.gang.app.ui.contract.ContractBean;
import com.zhong.gang.app.mvp.CommonPresenter;
import com.zhong.gang.applib.http.BaseDataBean;
import com.zhong.gang.applib.http.callback.DataCallBack;
import com.zhong.gang.applib.ui.matching.MatchingBean;
import java.io.File;


public class ZhuChangPresenter{
    private Activity activity;
    private ActivityZhuChangBinding binding;
    private ZhuChangMonenyController monenyController;
    private int snId;

    public ZhuChangPresenter(Activity activity, ActivityZhuChangBinding binding) {
        this.activity = activity;
        this.binding = binding;
        monenyController = new ZhuChangMonenyController(binding.mWeight,binding.pWeight,binding.kouWeight
                ,binding.jWeight,binding.jsWeight,binding.moneny
                ,binding.shoufaLayout,binding.zhangkouLayout);
    }


    public void setContractMaterial( ContractBean.ContractMaterialTypesBean bean){
        if (bean == null)
            return;
        binding.contractTv.setText(bean.getContractSn());
        snId = bean.getId();
    }


    public void statrt(File zjFile,File bdFile){
        MatchingBean matchingBean = binding.shipperView.getBean();
        if (matchingBean == null){
            ToastUtils.s(activity,"请选择货主");
            return;
        }
        double mWeight = monenyController.getWeight(binding.mWeight);
        double pWeight = monenyController.getWeight(binding.pWeight);
        if (mWeight <= 0){
            ToastUtils.s(activity,"请输入有效毛重");
            return;
        }if (pWeight <= 0){
            ToastUtils.s(activity,"请输入有效皮重");
            return;
        }
        if (zjFile == null && binding.buttonSwitch.isChecked()){
            ToastUtils.s(activity,"请选择质检图片");
            return;
        }
        if (bdFile == null){

            ToastUtils.s(activity,"请选择榜单照片");
            return;
        }
        double kWeight = monenyController.getWeight(binding.kouWeight);
        String list = binding.shoufaLayout.getJsonArray();
        if (TextUtils.isEmpty(list)){
            ToastUtils.s(activity,"请输入完整的料型信息");
            return;
        }
        double zhangkouMoneny = Math.abs(binding.zhangkouLayout.getMoneny());

        if (binding.buttonSwitch.isChecked())
            upZjPic(zjFile,bdFile,binding.shipperView.getBean().getConsignor().getId(),snId,mWeight,pWeight,"1",kWeight,list
                    ,zhangkouMoneny == 0?"0":(zhangkouMoneny > 0 ? "2":"1"),zhangkouMoneny+"");
        else
            upBdPic("",bdFile,binding.shipperView.getBean().getConsignor().getId(),snId,mWeight,pWeight,"0",kWeight,list
                    ,zhangkouMoneny == 0?"0":(zhangkouMoneny > 0 ? "2":"1"),zhangkouMoneny+"");
    }

    /*--------------------------------------------上传信息-------------------------------------------------------------------*/

    /**
     * 上传质检照片
     * @param zjFile
     * @param bdFile
     * @param consignorId
     * @param contractId
     * @param steelMillGrossWeight
     * @param steelMillTareWeight
     * @param qualityCheckResult
     * @param steelMillSlagWeight
     * @param materialTypeList
     * @param upDownType
     * @param upDownMoney
     */
    private void upZjPic(File zjFile,File bdFile,int consignorId,int contractId,double steelMillGrossWeight,double steelMillTareWeight
            ,String qualityCheckResult,double steelMillSlagWeight,String materialTypeList,String upDownType,String upDownMoney){
        CommonPresenter.upImg(activity,zjFile,new DataCallBack<BaseDataBean<String>>(activity){
            @Override
            public void onResponse(BaseDataBean<String> data) throws Exception {
                upBdPic(data.getResult(),bdFile,consignorId,contractId,steelMillGrossWeight
                        ,steelMillTareWeight,qualityCheckResult,steelMillSlagWeight,materialTypeList,upDownType,upDownMoney);
            }
        });
    }


    /**
     * 上传榜单照片
     * @param zjPath
     * @param bdFile
     * @param consignorId
     * @param contractId
     * @param steelMillGrossWeight
     * @param steelMillTareWeight
     * @param qualityCheckResult
     * @param steelMillSlagWeight
     * @param materialTypeList
     * @param upDownType
     * @param upDownMoney
     */
    private void upBdPic(String zjPath,File bdFile,int consignorId,int contractId,double steelMillGrossWeight,double steelMillTareWeight
            ,String qualityCheckResult,double steelMillSlagWeight,String materialTypeList,String upDownType,String upDownMoney){
        CommonPresenter.upImg(activity,bdFile,new DataCallBack<BaseDataBean<String>>(activity){
            @Override
            public void onResponse(BaseDataBean<String> data) throws Exception {
                super.onResponse(data);
                upMsg(zjPath,data.getResult(),consignorId,contractId,steelMillGrossWeight
                        ,steelMillTareWeight,qualityCheckResult,steelMillSlagWeight,materialTypeList,upDownType,upDownMoney);
            }
        });
    }

    /**
     * 上传信息后所有信息上传
     * @param steelMillQualityPhoto
     * @param steelMillPoundPhoto
     * @param consignorId
     * @param contractId
     * @param steelMillGrossWeight
     * @param steelMillTareWeight
     * @param qualityCheckResult
     * @param steelMillSlagWeight
     * @param materialTypeList
     * @param upDownType
     * @param upDownMoney
     */
    private void upMsg(String steelMillQualityPhoto,String steelMillPoundPhoto,int consignorId,int contractId,double steelMillGrossWeight
            ,double steelMillTareWeight,String qualityCheckResult,double steelMillSlagWeight,String materialTypeList,String upDownType,String upDownMoney){
        HttpUtils.post(Api.orderFactoryr,activity)
                .addParams("consignorId",consignorId+"")
                .addParams("contractId",contractId<=0?null:(contractId+""))
                .addParams("steelMillGrossWeight",steelMillGrossWeight+"")
                .addParams("steelMillTareWeight",steelMillTareWeight+"")
                .addParams("qualityCheckResult",qualityCheckResult+"")
                .addParams("steelMillSlagWeight",steelMillSlagWeight+"")
                .addParams("steelMillQualityPhoto",steelMillQualityPhoto+"")
                .addParams("steelMillPoundPhoto",steelMillPoundPhoto+"")
                .addParams("materialTypeList",materialTypeList)
                .addParams("upDownType",upDownType)
                .addParams("upDownMoney",upDownMoney).build()
                .execute(activity,"上传信息",new DataCallBack<BaseDataBean>(activity){
                    @Override
                    public void onResponse(BaseDataBean data) throws Exception {
                        super.onResponse(data);
                        activity.finish();
                    }
                });
    }
}
